/*
 * UpdateDatabaseAdapter.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.update;

import de.studiorutton.colorschemes.IColorSchemeMapper;
import de.studiorutton.gerengdic.Prefs;
import de.studiorutton.gerengdic.R;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UpdateDatabaseAdapter extends BaseAdapter {
	private final static String TAG = "learningwords";

	private UpdateDatabase ud;
	private IUpdateDatabaseListener udl;

	public UpdateDatabaseAdapter(UpdateDatabase ud) {
		this.ud = ud;

		int i = 0;
		while (i < ud.getUpdateDatabaseSize()) {
			ProgressUpdate pu = new ProgressUpdate();
			ud.setProgressListener(i, pu);
			i++;
		}
	}

	public void setUpdateDatabaseListener(IUpdateDatabaseListener udl) {
		this.udl = udl;
	}

	@Override
	public int getCount() {
		return ud.getUpdateDatabaseSize();
	}

	@Override
	public Object getItem(int position) {
		return ud.getUpdateFile(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	static class ViewHolder {
		LinearLayout layout;
		TextView title;
		TextView progressText;
		ProgressBar progressBar;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		IColorSchemeMapper csmi = Prefs.getInstance().getColorSchemeMapper();

		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from( udl.getContext()).inflate(
					R.layout.updatedatabaselistitemrow, parent, false );
			viewHolder = new ViewHolder();
			viewHolder.layout = (LinearLayout) convertView.findViewById( R.id.downloadframe );
			viewHolder.title = (TextView) convertView.findViewById( R.id.downloadtitle );
			viewHolder.progressText = (TextView) convertView.findViewById( R.id.downloadProgressNumber);
			viewHolder.progressBar = (ProgressBar) convertView.findViewById( R.id.downloadprogress );
			convertView.setTag( viewHolder );
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		UpdateFileTaskFacade uftf = ud.getUpdateFile( position );
		viewHolder.layout.setBackgroundDrawable(csmi.getUpdateItemBackground());
		viewHolder.title.setText( udl.getContext().getResources().getString( uftf.getTitleResource()));
		viewHolder.title.setTextColor( csmi.getUpdateItemTextcolor());
		viewHolder.progressText.setText( uftf.getProgressString() );
		viewHolder.progressText.setTextColor( csmi.getUpdateItemTextcolor());
		viewHolder.progressBar.setProgress( uftf.getProgress());
		
		return convertView;		
	}

	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}
	

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	class ProgressUpdate implements IUpdateTaskProgressListener {

		public void notifyProgress(int progress, long current, long total) {
			// Log.d( TAG, "ProgressUpdate:notifyProgress: progress = " +
			// progress );
			notifyDataSetChanged();

		}
	}

}
